

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.EntidadDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.*;

public class EntidadDao extends DAO {

    public EntidadDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, TIPO, CODIGO, NOMBRE, PALABRACLAVE FROM ENTIDAD WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return getDatos(resul, new EntidadDTO());
            }
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return null;
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, TIPO, CODIGO, NOMBRE, PALABRACLAVE FROM ENTIDAD");
            resul = prep.executeQuery();
            for (; resul.next(); c.add(getDatos(resul, new EntidadDTO()))) {
                ;
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        EntidadDTO dto = (EntidadDTO) obj;
        try {
            /*connectionIgFormato = */            getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO ENTIDAD(TIPO, CODIGO, NOMBRE, PALABRACLAVE, ID) VALUES(?, ?, ?, ?, ?)");
                dto.setId(getMaximoId("ENTIDAD"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE ENTIDAD SET TIPO=?, CODIGO=?, NOMBRE=?, PALABRACLAVE=? WHERE ID=?");
            }
            setDatos(prep,
                    dto,
                    "TIPO, CODIGO, NOMBRE, PALABRACLAVE, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);            
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM ENTIDAD WHERE ID = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM ENTIDAD WHERE NOMBRE = ?");
            prep.setString(1, ((EntidadDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return false;
    }
}
